﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.SqlClient;
using System.Data;

namespace SisLegal.Model
{   
    public class CrimeSubGeneric
    {
        public int ID { get; set; }
        public string Name { get; set; }
        public int IDGeneric { get; set; }
        public string Generic { get; set; }
        

        public CrimeSubGeneric()
        {
            this.ID = 0;
            this.Name = "";
            this.IDGeneric = 0;
            this.Generic = "";
        }
    }
    public class CrimeSubGenericManager
    {
        public List<CrimeSubGeneric> GetAll()
        {
            List<CrimeSubGeneric> result = new List<CrimeSubGeneric>();
            string sqlQuery = String.Format("SELECT * FROM CrimeSubGeneric ORDER BY ID");
            SqlConnection connection = new SqlConnection(Util.ConnKLegal);
            connection.Open();
            SqlCommand command = new SqlCommand(sqlQuery, connection);
            SqlDataReader dataReader = command.ExecuteReader();
            CrimeSubGeneric process = null;
            if (dataReader.HasRows)
            {
                while (dataReader.Read())
                {
                    process = new CrimeSubGeneric();
                    process.ID = Convert.ToInt32(dataReader["ID"]);
                    process.Name = dataReader["Name"].ToString();
                    result.Add(process);
                }
            }
            return result;
        }
        public Int32 Count()
        {
            List<CrimeSubGeneric> result = new List<CrimeSubGeneric>();
            string sqlQuery = String.Format("  SELECT COUNT(ID) AS total FROM CrimeSubGeneric");
            SqlConnection connection = new SqlConnection(Util.ConnKLegal);
            connection.Open();
            SqlCommand command = new SqlCommand(sqlQuery, connection);
            command.CommandType = CommandType.Text;
            Int32 countStart = (Int32)command.ExecuteScalar();
            return countStart;
        }

        public List<CrimeSubGeneric> Paginate(Int32 pag)
        {
            List<CrimeSubGeneric> result = new List<CrimeSubGeneric>();
            string sqlQuery = String.Format("SELECT * FROM (" +
                "SELECT ROW_NUMBER() OVER(ORDER BY csg.ID) AS NUMBER, csg.ID, csg.Name, csg.IDCrimeGeneric, cg.Name as Generic FROM dbo.CrimeSubGeneric csg "+
                "INNER JOIN dbo.CrimeGeneric cg ON csg.IDCrimeGeneric = cg.ID " +
                ")as tbl " +
                "WHERE NUMBER BETWEEN((" + Convert.ToString(pag) + "-1) * (10 +1)) AND (" + Convert.ToString(pag) + " * 10) ");

            SqlConnection connection = new SqlConnection(Util.ConnKLegal);
            connection.Open();
            SqlCommand command = new SqlCommand(sqlQuery, connection);
            SqlDataReader dataReader = command.ExecuteReader();
            CrimeSubGeneric process = null;
            if (dataReader.HasRows)
            {
                while (dataReader.Read())
                {
                    process = new CrimeSubGeneric();
                    process.ID = Convert.ToInt32(dataReader["ID"]);
                    process.Name = dataReader["Name"].ToString();
                    process.IDGeneric = Convert.ToInt32(dataReader["IDCrimeGeneric"]);
                    process.Generic = dataReader["Generic"].ToString();
                    result.Add(process);
                }
            }
            return result;
        }

        public int Save(CrimeSubGeneric obj)
        {
            string createQuery = String.Format("Insert into CrimeSubGeneric (Name,IDCrimeGeneric) Values('{0}','{1}');"
                + "Select @@Identity", obj.Name, obj.IDGeneric);

            SqlConnection connection = new SqlConnection(Util.ConnKLegal);
            connection.Open();
            SqlCommand command = null;

            command = new SqlCommand(createQuery, connection);

            int saveID = 0;
            try
            {
                var commandResult = command.ExecuteScalar();
                if (commandResult != null)
                {
                    saveID = Convert.ToInt32(commandResult);
                }
            }
            catch (Exception ex)
            {

            }

            command.Dispose();
            connection.Close();
            connection.Dispose();

            return saveID;
        }

        public int Update(CrimeSubGeneric obj)
        {
            string updateQuery = String.Format("Update CrimeSubGeneric SET Name='{0}' Where ID = {1};",
               obj.Name, obj.ID);

            SqlConnection connection = new SqlConnection(Util.ConnKLegal);
            connection.Open();
            SqlCommand command = null;
            command = new SqlCommand(updateQuery, connection);

            int saveID = 0;
            try
            {
                var commandResult = command.ExecuteScalar();
                if (commandResult != null)
                {
                    saveID = Convert.ToInt32(commandResult);
                }
                else
                {
                    saveID = obj.ID;
                }
            }
            catch (Exception ex)
            {

            }
            command.Dispose();
            connection.Close();
            connection.Dispose();

            return saveID;
        }

        public CrimeSubGeneric GetById(int articleId)
        {
            CrimeSubGeneric result = new CrimeSubGeneric();
            string sqlQuery = String.Format("select * from CrimeSubGeneric where ID={0}", articleId);
            SqlConnection connection = new SqlConnection(Util.ConnKLegal);
            connection.Open();
            SqlCommand command = new SqlCommand(sqlQuery, connection);
            SqlDataReader dataReader = command.ExecuteReader();

            if (dataReader.HasRows)
            {
                while (dataReader.Read())
                {
                    result.ID = Convert.ToInt32(dataReader["ID"]);
                    result.Name = dataReader["Name"].ToString();
                }
            }

            return result;
        }

        public bool Delete(int articleId)
        {
            bool result = false;
            string sqlQuery = String.Format("delete from CrimeSubGeneric where ID = {0}", articleId);

            SqlConnection connection = new SqlConnection(Util.ConnKLegal);
            connection.Open();
            SqlCommand command = new SqlCommand(sqlQuery, connection);
            int rowsDeletedCount = command.ExecuteNonQuery();
            if (rowsDeletedCount != 0)
                result = true;
            command.Dispose();
            connection.Close();
            connection.Dispose();
            return result;
        }
    }
}